package room

import (
	"xkginweb/global"
	"xkginweb/model/entity/room"
	"xkginweb/model/entity/room/request"
)

// 定义room的service提供room的数据curd的操作

type RoomService struct{}

// @author: hcl
// @function: CreateRoom
// @description: 创建房间
// @param: room.QccRoom
// @return: err error

func (service *RoomService) CreateRoom(room *room.QccRoom) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.KSD_DB.Create(room).Error
	return err
}

//@author: hcl
//@function: UpdateRoom
//@description: 更新房间
//@param: room.QccRoom
//@return: err error

func (service *RoomService) UpdateRoom(room *room.QccRoom) (err error) {
	//err = global.KSD_DB.Save(room).Error
	err = global.KSD_DB.Model(room).Updates(room).Error
	return err
}

// @author: hcl
// @function: DeleteRoom
// @description: 删除房间
// @param: room.QccRoom
// @return: err error

func (service *RoomService) DeleteRoom(room *room.QccRoom) (err error) {
	err = global.KSD_DB.Delete(&room).Error
	return err
}

// @author: hcl
// @function: DeleteRoomById
// @description: 根据ID删除房间
// @param: uint
// @return: err error

func (service *RoomService) DeleteRoomById(id uint) (err error) {
	var room room.QccRoom
	err = global.KSD_DB.Where("room_id = ?", id).Delete(&room).Error
	return err
}

// @author: hcl
// @function: GetRoom
// @description: 根据ID获取房间信息
// @param: id uint
// @return: room *room.QccRoom, err error

func (service *RoomService) GetRoom(id uint) (room *room.QccRoom, err error) {
	err = global.KSD_DB.Where("room_id = ?", id).First(&room).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadRoomPage
//@description: 分页获取房间列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *RoomService) LoadRoomPage(info request.RoomPageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&room.QccRoom{})

	// 准备切片帖子数组
	var RoomList []room.QccRoom

	//if info.Status != -1 {
	//	db = db.Where("status = ?", info.Status)
	//}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(room_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return RoomList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Find(&RoomList).Error
	}

	// 结果返回
	return RoomList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadRoom
//@description: 分页获取房间
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *RoomService) LoadRoom(info request.RoomPageInfoList) (list []*room.QccRoom, err error) {

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&room.QccRoom{})

	// 准备切片帖子数组
	list = []*room.QccRoom{}

	//if info.Status != -1 {
	//	db = db.Where("status = ?", info.Status)
	//}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(room_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 执行查询
	err = db.Find(&list).Error

	// 结果返回
	return list, err
}
